package com.hl.web;

import com.alibaba.fastjson.JSON;
import com.hl.dao.CommunityDaoImpl;
import com.hl.pojo.PanghuCommunity;
import com.hl.util.GeneralResult;
import com.hl.util.PageResult;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/community/communityList")
public class CommunityList extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数  name=1&startTime=2024-06-24+00:00:00&
        // endTime=2024-06-24+00:00:00&pageNum=1&pageSize=10&communityName=
        String name = req.getParameter("name");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        //调用sql，查询数据
        CommunityDaoImpl dao = new CommunityDaoImpl();
        List<PanghuCommunity> list = dao.search(name,startTime,endTime,pageNum,pageSize);

        for (PanghuCommunity panghuCommunity : list) {
            Integer communityId = panghuCommunity.getCommunityId();
            Integer user = dao.findUser(communityId);
            Integer shop = dao.findShop(communityId);
            panghuCommunity.setCommunityUserCount(user);
            panghuCommunity.setCommunityShopCount(shop);
        }
        //封装数据  status，和data分页结果集
        PageResult pageResult = new PageResult(list);

        GeneralResult result = new GeneralResult(200,"查询成功",pageResult);
        //返回数据
        resp.setContentType("application/json;charset=utf-8");
        //JSON.toJSONString(result)  把任意类型数据转为json格式字符串
        resp.getWriter().append(JSON.toJSONString(result));
    }
}
